package com.zmn.oms.third.tc58;

import com.zmn.oms.third.tc58.dto.api.Tc58Order;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 类描述：58同城常量类
 *
 * @author LiangHailong
 * @date 2019/09/24 16:43
 */
@Component
public final class Tc58Consts {

    public static final String ORDER_TYPE_ONE = "1";    //一段订单
    public static final String ORDER_TYPE_TWO = "2";    //二段订单

    public static boolean POWER_ON; // 58同城VIP服务接入总开关

    public static String APPKEY;
    public static String APPKEY_JX;
    public static String SECRET;
    public static String API_URL;

    public static final String CHANNEL_NAME = "58同城114";

    // 尾款字段名   在获取支付链接message返回的字符串
    public static final String LAST_PAY_MONEY ="lastPayMoney";

    /* 渠道ID */
    public static final int CHANNEL_ID_VIP = 31067; // 58同城VIP（58同城114管家）
    public static final int CHANNEL_ID_FWK = 31096; // 58同城服务卡
    public static final int CHANNEL_ID_JX = 1522;   // 到家精选3.0
    public static final int CHANNEL_ID_JX_PIPE = 33727; // 精选3.0疏通
    public static final int CHANNEL_ID_JX_CLOCK = 33930;    // 精选3.0开锁
    public static final int CHANNEL_ID_JX_APARTMENT = 41737; // 同城公寓

    /* 接口地址路径 */
    public static final String PATH_QUERY_ORDER = "/steward/order/searchorder"; // 订单查询
    public static final String PATH_QUERY_ORDER_JX = "/steward/order/searchorder/V1";   // 到家精选订单查询
    public static final String PATH_ACCEPT_ORDER = "/steward/order/acceptorder";    // 确认接单
    public static final String PATH_ACCEPT_ORDER_JX = "/steward/order/acceptorder/V1";  // 到家精选确认接单
    public static final String PATH_MASTER_INFO = "/steward/order/staffinfo";   // 师傅信息
    public static final String PATH_ORDER_STATUS = "/steward/order/servicestate";   // 状态同步
    public static final String PATH_PAY_LINK = "/steward/order/getPayLink"; // 获取支付链接

    /* 58订单的来源类型 */
    public static final String ORDER_SOURCE_TYPE_FWK = "2";
    public static final String ORDER_SOURCE_TYPE_JX = "3";

    /**
     * 获取所属渠道
     * @param channelId
     * @return
     */
    public static ChannelInfo getChannelInfo(Integer channelId) {
        int iChannelId = Optional.ofNullable(channelId).orElse(-1);

        if (CHANNEL_ID_VIP == iChannelId) {
            return new ChannelInfo(CHANNEL_ID_VIP, "58同城114", false);
        }
        else if (CHANNEL_ID_FWK == iChannelId) {
            return new ChannelInfo(CHANNEL_ID_FWK, "58同城服务卡", false);
        }
        else if (CHANNEL_ID_JX == iChannelId) {
            return new ChannelInfo(CHANNEL_ID_JX, "58同城-到家精选", true);
        }
        else if (CHANNEL_ID_JX_PIPE == iChannelId) {
            return new ChannelInfo(CHANNEL_ID_JX_PIPE, "58同城-到家精选-管道疏通", true);
        }
        else if (CHANNEL_ID_JX_CLOCK == iChannelId) {
            return new ChannelInfo(CHANNEL_ID_JX_CLOCK, "58同城-到家精选-开锁", true);
        }
        else {
            return new ChannelInfo(CHANNEL_ID_VIP, "58同城114", false);
        }
    }

    /**
     * 获取所属渠道
     * @param tc58Order
     * @return
     */
    public static ChannelInfo getChannelInfo(Tc58Order tc58Order) {
        String sourceType = tc58Order.getSourceType();

        if (ORDER_SOURCE_TYPE_JX.equals(sourceType)) {
            if (Tc58Mapper.isJxPipeProduct(tc58Order.getSkuId())) {
                return new ChannelInfo(CHANNEL_ID_JX_PIPE, "58同城-到家精选-管道疏通", true);
            }
            else if (Tc58Mapper.isJxUnlockProduct(tc58Order.getSkuId())) {
                return new ChannelInfo(CHANNEL_ID_JX_CLOCK, "58同城-到家精选-开锁", true);
            }
            else {
                return new ChannelInfo(CHANNEL_ID_JX, "58同城-到家精选", true);
            }
        }
        else if (ORDER_SOURCE_TYPE_FWK.equals(sourceType)) {
            return new ChannelInfo(CHANNEL_ID_FWK, "58同城服务卡", false);
        }
        else {
            return new ChannelInfo(CHANNEL_ID_VIP, "58同城114", false);
        }
    }


    @Value("${tc58.vip.power-on:false}")
    public void setPowerOn(boolean powerOn) {
        POWER_ON = powerOn;
    }
    @Value("${tc58.vip.clientid:null}")
    public void setAppkey(String appkey) {
        APPKEY = appkey;
    }
    @Value("${tc58.jingxuan.clientid:null}")
    public void setAppkeyJx(String appkey) {
        APPKEY_JX = appkey;
    }
    @Value("${tc58.vip.secretkey:null}")
    public void setSecret(String secret) {
        SECRET = secret;
    }
    @Value("${tc58.vip.url.api-invok:null}")
    public void setApiUrl(String apiUrl) {
        API_URL = apiUrl;
    }

    /**
     * 58同城相关渠道信息
     */
    @Data
    public static class ChannelInfo {
        /**
         * 渠道ID
         */
        private int id;
        /**
         * 渠道名称
         */
        private String name;
        /**
         * 是否是到家精选渠道订单
         */
        private boolean jingxuan;

        public ChannelInfo(int id, String name, boolean jingxuan) {
            this.id = id;
            this.name = name;
            this.jingxuan = jingxuan;
        }
    }
}
